package icu.lookyousmileface.bio;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.StringJoiner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author shenj
 * @title: ServerBio
 * @projectName NettyPro
 * @date 2020/11/30 10:41
 */
@SuppressWarnings("ALL")
public class ServerBio {

    private static final int port = 7748;

    /**
     * @author shenj
     * @title: handle
     * @date 2020/11/30  14:09
     * server处理来自client的socket发送的信息
     */
    public static void handle(Socket socket) throws IOException {

        InputStream socketInputStream = socket.getInputStream();

        byte[] data = new byte[1024];

        int len;

        while (true) {
            int read = socketInputStream.read(data);

            if (!(-1 != (len = read))) break;

            System.out.println(new String(data, 0, len));


        }
    }

    /**
     * @author shenj
     * @title: main
     * @date 2020/11/30  14:09
     */
    public static void main(String[] args) throws IOException {

//        String sra = "Java";
//        String srd = "Hello";
//        String src = "World";
//
//        System.out.println("[" + sra + "|" + srd + "|" + src + "]");
//
//        StringBuilder sb = new StringBuilder();
//        sb.append("[").append(sra).append("|").append(srd).append("|").append(src).append("]");
//        System.out.println(sb.toString());
//
//        StringJoiner stringJoiner = new StringJoiner("|", "[", "]");
//        stringJoiner.add(sra).add(srd).add(src);
//        System.out.println(stringJoiner.toString());
//
//        String join = String.join("|", sra, srd, src);
//        StringJoiner stringJoiner1 = new StringJoiner("|", "[", "]");
//        stringJoiner1.add(join);
//        System.out.println(stringJoiner1);
//
//        ArrayList<String> strings = new ArrayList<>();
//        strings.add(sra);
//        strings.add(srd);
//        strings.add(src);
//
//        String join1 = String.join("|", strings);
//        System.out.println(join1);
//
//        System.out.println(String.join("|", new ArrayList<String>(16){{
//            add(sra);
//            add(srd);
//            add(src);
//        }}));

        ExecutorService threadpool = Executors.newCachedThreadPool();

        ServerSocket serverSocket = new ServerSocket(port);

        System.out.println("Server status:Runing!");

        while (true){

            final Socket socket = serverSocket.accept();

            System.out.println("一个客户端连接到来！");

            threadpool.execute(()->{

                try {

                    handle(socket);

                }
                catch (IOException e){

                    e.printStackTrace();

                }});

        }


    }
}


